<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Space extends MY_Controller {
	private $checkkey='mcheckcode';

	private $master;
	function __construct(){
		parent::__construct();
	}

	private function initspace($uid){
		$this->master=$this->Data_model->getSingle(array('mid'=>(int)$uid),'member'); //会员信息

		if(empty($this->master)){
			if($this->input->is_ajax_request()){
				echo json_encode(array('errorcode'=>1,'error'=>'访问的功能不存在'));
			}else{
				$this->showMessage('页面不存在','您访问的页面不存在');
			}
		}
		if($this->master['mid']==$this->member->mid){
			switch ($this->uri->segment(2)) {
				case 'works':
				case 'work':
					$url='member/'.$this->uri->segment(2).'/'.$this->uri->segment(3);
					break;
				case 'message':
					$url='member/'.$this->uri->segment(2);
					break;
				default:
					$url='member/'.$this->uri->segment(3);
					break;
			}
			redirect($url);
		}

		$this->master['data']=$this->Data_model->getSingle(array('mid'=>(int)$uid,'isdefault'=>1),'member_data');
	}

	function index(){
		$mid=$this->uri->segment(2);
		$this->initspace($mid);
		
		//作品列表
		$where=array('mid'=>$this->master['mid']);
		$worklist=$this->Data_model->getData($where,'iscomm DESC,id DESC',0,0,'works');
		foreach ($worklist as $key => $value) {
			$worklist[$key]['puttime']=date('Y.m.d',$value['puttime']);
			$worklist[$key]['content']=$value['content'];
			$worklist[$key]['href']=site_url('space/work/'.$this->master['mid'].'/'.$value['id']);
		}
		$worklist=$this->getExtends($worklist); 
		//print_r($worklist);
		$ud=$this->getUnionData($worklist);
		foreach ($worklist as $key => $value) {
			if(!empty($ud['goods'][$value['id']])){
				$worklist[$key]['gooded']=$ud['goods'][$value['id']];
			}
			if(!empty($ud['watchs'][$value['id']])){
				$worklist[$key]['watched']=$ud['watchs'][$value['id']];
			}
		}
		
		//留言列表
		/*$where=array('cid'=>$this->master['mid'],'issys'=>0,'reid'=>0);
		$this->Data_model->db->from('mem_comm mc')->select('mc.*,m.mname,m.mnick,m.avatar')->join('member m','m.mid=mc.mid','left');
		$msglist=$this->Data_model->getData($where,'mc.id DESC',0,5);*/


		$this->sview('main',array(
			'currentnav'=>$this->master['mnick'],
			'worklist'=>$worklist,
			//'msglist'=>$msglist,
				'latest'=>$this->getLatest()
			));
	}

	function works(){
		$mid=$this->uri->segment(3);
		$this->initspace($mid);

		$page=$this->uri->segment(4);
		
		$where=array('mid'=>$mid);
		if(!empty($lastid)){
			$where['id <']=$lastid;
		}

		$worklist=$this->Data_model->getData($where,'id DESC',0,5,'works');
		foreach ($worklist as $key => $work) {
			$worklist[$key]['puttime']=date('Y.m.d',$work['puttime']);
		}

		$worklist=$this->getExtends($worklist);
		
		$this->sview('works',array(
			'currentnav'=>'作品列表',
			'wlist'=>$worklist,
				'latest'=>$this->getLatest()
			));
	}

	function work(){
		$mid=$this->uri->segment(3);
		$this->initspace($mid);

		$wid=$this->uri->segment(4);

		$work = $this->Data_model->getSingle(array('id'=>(int)$wid,'mid'=>$mid),'works');
		if(empty($work)){
			$this->showMessage('作品未找到','您查看的作品不存在或已删除！');
		}

		$work['images']=$this->Data_model->getData(array('wid'=>$work['id']),'id ASC',0,0,'work_image');
		$work['puttime']=date('Y.m.d',$work['puttime']);
		

		$where=array('wid'=>$wid,'reid'=>0);
		$this->Data_model->db->from('work_comm wc')->select('wc.*,m.mname,m.mnick,m.avatar')->join('member m','m.mid=wc.mid','left');
		$msglist=$this->Data_model->getData($where,'wc.id DESC',0,5);
		$this->workview();
		
		//回复
		foreach($msglist as $key=> $val){
			$where=array('wid'=>$val['wid'],'reid'=>$val['id']);
			$this->Data_model->db->from('work_comm wc')->select('wc.*,m.mname,m.mnick,m.avatar')->join('member m','m.mid=wc.mid','left');
			$remsglist=$this->Data_model->getData($where,'wc.id DESC',0,1);	
			$msglist[$key]['remsglist']=array();
			if(count($remsglist)>0){
				$msglist[$key]['remsglist']=$remsglist[0];
			}
			
		}
		
		//获取其他文章
		$where=array('mid'=>$mid);
		$worklist=$this->Data_model->getData($where,'iscomm DESC,id DESC',0,5,'works');

		foreach ($worklist as $key => $value) {
			$worklist[$key]['puttime']=date('Y.m.d',$value['puttime']);
			$worklist[$key]['content']=$value['content'];
			$worklist[$key]['href']=site_url('space/work/'.$this->master['mid'].'/'.$value['id']);
		}
		$worklist=$this->getExtends($worklist);
		$ud=$this->getUnionData($worklist);
		foreach ($worklist as $key => $value) {
			if(!empty($ud['goods'][$value['id']])){
				$worklist[$key]['gooded']=$ud['goods'][$value['id']];
			}
			if(!empty($ud['watchs'][$value['id']])){
				$worklist[$key]['watched']=$ud['watchs'][$value['id']];
			}
		}
		
		//留言列表
		$where=array('cid'=>$mid,'issys'=>0,'reid'=>0);
		$this->Data_model->db->from('mem_comm mc')->select('mc.*,m.mname,m.mnick,m.avatar')->join('member m','m.mid=mc.mid','left');
		$allmsglist=$this->Data_model->getData($where,'mc.id DESC',0,5);

		
		$this->sview('work',array(
			'currentnav'=>'作品详情',
			'work'=>$work,
			'msglist'=>$msglist,
			'worklist'=>$worklist,
			'allmsglist'=>$allmsglist,
			'latest'=>$this->getLatest()
			));
	}

	function message(){
		$mid=$this->uri->segment(3);
		$this->initspace($mid);
		$post=$this->input->post(NULL,TRUE);
		if(!empty($post)){
			$this->needLogin();
			/*if(!$this->checkLogin()){
				echo json_encode(array('errorcode'=>1,'message'=>'请先登录！'));
				exit;
			}*/

			$data = array_intersect_key($post,array('content'=>'','cid'=>'','reid'=>'','ispillow'));
			if(empty($data['content'])){
				echo json_encode(array('errorcode'=>1,'message'=>'您还没填写内容呢！'));
				exit;
			}
			if(isset($data['reid']))$data['reid']=(int)$data['reid'];
			else $data['reid']=0;
			if($mid == $this->member->mid && ($data['reid']==0)){
				echo json_encode(array('errorcode'=>1,'message'=>'您是太寂寞了吧，给自己留言！'));
				exit;
			}
			if(empty($data['ispillow'])){
				unset($data['ispillow']);
			}else{
				$data['ispillow']=0;
			}

			$data['puttime']=time();
			$data['cid']=$mid;
			$data['mid']=$this->member->mid;
			$data['ip']=$this->input->ip_address();
			$this->Data_model->addData($data,'mem_comm');
			$this->Data_model->db->where(array('mid'=>(int)$mid))->set('commnum','commnum+1',FALSE)->update('member');
			echo json_encode(array('errorcode'=>0,'message'=>empty($data['reid'])?'留言成功！':'回复成功！','url'=>'refresh'));
		}else{
			//读取消息列表
			$page=$this->uri->segment(4);

			$where=array('mc.cid'=>$this->master['mid'],'mc.issys'=>0,'mc.reid'=>0,'mc.ispillow'=>0);

			$this->Data_model->db->from('mem_comm mc')
				->select('mc.*,m.mname,m.mnick,m.avatar,md.title as mtitle')
				->join('member m','m.mid=mc.mid','left')
				->join('member_data md','m.mid=md.mid','left');

			$msglist=$this->Data_model->getData($where,'mc.id DESC',0,5);

			foreach ($msglist as $key => $msg) {
				$msglist[$key]['mnick']=empty($msg['mnick'])?$msg['mname']:$msg['mnick'];
				$msglist[$key]['puttime']=date('Y-m-d H:i:s',$msg['puttime']);
				if($msg['mid']==$this->member->mid){
					$msglist[$key]['spaceurl']=site_url('member');
				}else{
					$msglist[$key]['spaceurl']=site_url('space/'.$msg['mid']);
				}
			}
	
			$this->sview('message',array(
				'currentnav'=>'空间留言',
				'msglist'=>$msglist
				));
		}
	}

	//
	function workgood(){
		$mid=$this->uri->segment(3);
		$wid=$this->uri->segment(4);

		$result=array();
		$this->needLogin();
		if(empty($wid)){
			exit;
		}
		$row=$this->Data_model->getSingle(array('wid'=>$wid,'mid'=>$this->member->mid),'work_good');
		if(!empty($row)){
			$result['errorcode']=1;
			$result['message']='您已赞过了';
		}else{
			$data['wid']=$wid;
			$data['mid']=$this->member->mid;
			$this->Data_model->addData($data,'work_good');
			$this->Data_model->db->where(array('id'=>(int)$wid))->set('goodnum','goodnum+1',FALSE)->update('works');
			$result['errorcode']=0;
			$result['message']='';
		}

		
		echo json_encode($result);
	}

	function workwatch(){
		$mid=$this->uri->segment(3);
		$wid=$this->uri->segment(4);

		$result=array();
		$this->needLogin();
		if(empty($wid)){
			exit;
		}
		$row=$this->Data_model->getSingle(array('wid'=>$wid,'mid'=>$this->member->mid),'work_watch');
		if(!empty($row)){
			$result['errorcode']=1;
			$result['message']='您已关注过了';
		}else{
			$data['wid']=$wid;
			$data['mid']=$this->member->mid;
			$this->Data_model->addData($data,'work_watch');
			$this->Data_model->db->where(array('id'=>(int)$wid))->set('watchnum','watchnum+1',FALSE)->update('works');
			$result['errorcode']=0;
			$result['message']='';
		}
		echo json_encode($result);
	}

	//评论
	function workcomm(){
		$mid=$this->uri->segment(3);
		$wid=$this->uri->segment(4);
		$post=$this->input->post(NULL,TRUE);
		$result=array();
		$this->needLogin();
		$data = array_intersect_key($post,array('content'=>'','cid'=>'','reid'=>''));
		if(empty($data['content'])){
			echo json_encode(array('errorcode'=>1,'message'=>'您还没填写内容呢！'));
			exit;
		}
		if(isset($data['reid']))$data['reid']=(int)$data['reid'];
		else $data['reid']=0;
		//echo $data['reid'];
		if($mid == $this->member->mid && $data['reid']==0){
			echo json_encode(array('errorcode'=>1,'message'=>'您不能评论自己作品！'));
			exit;
		}

		$data['puttime']=time();
		$data['wid']=$wid;
		$data['mid']=$this->member->mid;
		$data['ip']=$this->input->ip_address();
		$this->Data_model->addData($data,'work_comm');
		$this->Data_model->db->where(array('id'=>(int)$wid))->set('commnum','commnum+1',FALSE)->update('works');
		echo json_encode(array('errorcode'=>0,'message'=>empty($data['reid'])?'评论成功！':'回复成功！','url'=>'1'));
	}

	function workview(){
		$wid=$this->uri->segment(4);

		$this->Data_model->db->where(array('id'=>(int)$wid))->set('viewnum','viewnum+1',FALSE)->update('works');
	}

	function workshare(){
		$wid=$this->uri->segment(4);

		$this->Data_model->db->where(array('id'=>(int)$wid))->set('shares','shares+1',FALSE)->update('works');
		echo json_encode(array('errorcode'=>0));
	}
	
	//空间页面子模板调用
	function sview($tpl='main',$data=array()){
		$data['subtpl']=$tpl;
		$data['master']=$this->master;
		$this->view('space',$data);
		
	}
}